package cn.com.czpb.client.module.personal.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import cn.com.czpb.client.R;
import cn.com.czpb.client.model.event.ChangeVideoStartEvent;
import cn.com.czpb.client.module.MainActivity;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.SPUtil;

import butterknife.BindView;
import de.greenrobot.event.EventBus;

public class WifiDialog extends Dialog implements View.OnClickListener {

    @BindView(R.id.tv_1)
    TextView tv_1;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_2)
    TextView tv_2;
    @BindView(R.id.tv_3)
    TextView tv_3;

    TextView cancel;

    private Activity activity;
    private View view;
    private SPUtil spu;

    private int type;

    public WifiDialog(Context context) {
        super(context);
        activity = (Activity) context;
        spu = SPUtil.getInstance();

    }


    public WifiDialog(Context context, int theme, int type) {
        super(context, theme);
        activity = (Activity) context;
        spu = SPUtil.getInstance();

        this.type = type;
        init();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        init();
    }


    private void init() {
        WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        LogUtils.i("width-->" + width);
        view = LayoutInflater.from(activity).inflate(R.layout.dialog_wifi, null);

        setContentView(view);
        tv_1 = view.findViewById(R.id.tv_1);
        tv_2 = view.findViewById(R.id.tv_2);

        tv_title = view.findViewById(R.id.wifi_title);
        cancel = view.findViewById(R.id.cancel);
        tv_1.setOnClickListener(this);
        tv_2.setOnClickListener(this);
        cancel.setOnClickListener(this);

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.END | Gravity.BOTTOM);
        lp.width = width;
        dialogWindow.setAttributes(lp);

        if (type == 0) {//非wifi网络流量
            tv_title.setText("非Wifi网络流量");
            tv_1.setText("最佳效果(下载大图)");

            tv_2.setText("极省流量(不下载图)");


        } else {
            tv_title.setText("非Wifi网络播放提醒");
            tv_1.setText("每次提醒");
            tv_2.setText("提醒一次");

        }

    }


    @Override
    public void onClick(View view) {
        Intent intent=new Intent();
        switch (view.getId()) {
            case R.id.tv_1:
                if (type == 1) {
                    spu.setWifi("1");
                    spu.setIsNeedNotity(true);
                } else {
                    spu.setShowImage("0");
                    intent.setAction(MainActivity.SHOW);

                }

                dismiss();
                break;
            case R.id.tv_2:
                if (type == 1) {
                    spu.setWifi("0");
                } else {
                    spu.setShowImage("1");
                    intent.setAction(MainActivity.UNSHOW);
                }
                dismiss();
                break;

            case R.id.cancel:
                dismiss();
                break;
            default:
                break;
        }
        activity.sendBroadcast(intent);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        ChangeVideoStartEvent event = new ChangeVideoStartEvent();
        event.setVideoChange(true);
        EventBus.getDefault().post(event);


    }

}